<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all" />
    <script th:src="@{/layui/layui.js}"></script>
    <script th:src="@{/js/register/jquery-3.1.0.min.js}"></script>
</head>
<body>
    <input id="hidden" type="hidden" th:value="${id}">
    <center>
        <button type="button" style="margin-top: 20px;" class="layui-btn" id="test1">图片上传</button>
        <div class="layui-upload" >
            <div class="layui-upload-list">
                <img style="width: 200px;height: 200px" class="layui-upload-img" id="demo1">
                <p id="demoText"></p>
            </div>
        </div>
    </center>
</body>

<script>
    layui.use(['upload','jquery'], function(){
        var $ = layui.jquery
            ,upload = layui.upload;

        var id2 = $("#hidden").val();

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '/aduitlog/upload?id='+id2 //改成您自己的上传接口
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(!res){
                    return layer.msg('上传失败');
                }
                //上传成功
                layer.msg(JSON.stringify("上传成功"));
                setTimeout(function () {
                    //先得到当前iframe层的索引
                    var index = parent.layer.getFrameIndex(window.name);
                    //再执行关闭
                    parent.layer.close(index);
                    window.parent.location.reload();
                }, 2000);
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
    })


</script>
</html>